//
//  GuanYuViewController.m
//  TravelYo
//
//  Created by lanou3g on 15/11/2.
//  Copyright © 2015年 TravelYo. All rights reserved.
//

#import "GuanYuViewController.h"
#import "AppDelegate.h"

@interface GuanYuViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UIView *nightModeView;

@property (nonatomic, strong) NSArray *titleArray;

@end

@implementation GuanYuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleArray = @[@"清除缓存", @"夜间模式", @"分享", @"免责声明",  @"关于我们"];
    self.navigationController.navigationBar.hidden = YES;
    [self addSubViews];
    // Do any additional setup after loading the view.
}


- (void)addSubViews
{
//    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
//    imageView.image = [UIImage imageNamed:@"BEIjing.png"];
//    // imageView.alpha = 0.;
//    [self.view addSubview:imageView];
    
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:(UITableViewStyleGrouped)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
     _tableView.alpha = 0.8;
     _tableView.tableFooterView.hidden = YES;
    _tableView.bounces = NO;
    [self.view addSubview:_tableView];
    
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 300)];
    imageV.image = [UIImage imageNamed:@"shijie.png"];
    _tableView.tableHeaderView = imageV;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifier];
    }
    if (indexPath.row == 1) {
        UISwitch *YejianSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
        [YejianSwitch setOn:NO];
        [YejianSwitch addTarget:self action:@selector(yejianMoShi:) forControlEvents:(UIControlEventValueChanged)];
        cell.accessoryView = YejianSwitch;
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = self.titleArray[indexPath.row];
    
    return cell;
}

// 夜间模式
- (void)yejianMoShi:(UISwitch *)yejianSwitch
{
    _nightModeView = [[UIView alloc] initWithFrame:self.view.frame];
    _nightModeView.backgroundColor = [UIColor blackColor];
    _nightModeView.tag = 1;
    _nightModeView.alpha = 0.5;
    _nightModeView.userInteractionEnabled = NO;
    if (yejianSwitch.on == YES) {
       [[UIApplication sharedApplication].keyWindow addSubview:_nightModeView];
    } else if (yejianSwitch.on == NO){
        for (id tempView in [UIApplication sharedApplication].keyWindow.subviews) {
            if ([tempView isKindOfClass:[UIView class]]) {
                UIView *view = (UIView *)tempView;
                if (view.tag == 1) {
                    [view removeFromSuperview];
                    break;
                }
            }
        }
        [_nightModeView removeFromSuperview];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    // 清除缓存
    if (indexPath.row == 0) {
        // 计算缓存大小
        float folderSize;
        folderSize += [[SDImageCache sharedImageCache] getSize] / 1024.0 / 1024.0;
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:[NSString stringWithFormat:@"缓存大小为%.2fM.确定要清理缓存吗?",folderSize] preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *defaulT = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
         UIAlertController *alertb = [UIAlertController alertControllerWithTitle:@"友情提示" message:@"清除缓存成功" preferredStyle:UIAlertControllerStyleAlert];
            // 清除缓存
            [[SDImageCache sharedImageCache] clearDisk];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self dismissViewControllerAnimated:YES completion:nil];
            });
            [self presentViewController:alertb animated:YES completion:nil];
        }];
        UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alert addAction:defaulT];
        [alert addAction:cancle];
        [self presentViewController:alert animated:YES completion:nil];
    } else if (indexPath.row == 2) {
        
      [UMSocialSnsService presentSnsIconSheetView:self appKey:@"5638791567e58e3b80002375" shareText:@"你想说的话:"
                                              shareImage:[UIImage
                                              imageNamed:@"share.png"]
                                              shareToSnsNames:[NSArray
                                              arrayWithObjects:
                                              UMShareToSina,
                                              UMShareToRenren,
                                              UMShareToTencent,
                                              UMShareToDouban,
                                              UMShareToSms,
                                              UMShareToEmail,
                                              UMShareToFacebook,nil]
                                              delegate:nil];
        
        } else if (indexPath.row == 3) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"友情提示" message:@"TravelYo 部分数据来自网络, 本APP不用于盈利性质的商业软件" preferredStyle:(UIAlertControllerStyleActionSheet)];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:YES completion:nil];
        });
        [self presentViewController:alert animated:YES completion:nil];
    } else if (indexPath.row == 4) {
        OurViewController *ourVC = [[OurViewController alloc] init];
        ourVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:ourVC animated:YES];
  }
    
}
    

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigati

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destina[UIApplication sharedApplication].keyWindowtionViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
